/**
 * Created by admin on 2017/9/7.
 */
layui.use(['element', 'form', 'collage', 'pageFlip', 'global'], function () {
    var element = layui.element;
    var form = layui.form;
    $ = layui.jquery;
    var global = layui.global;
    var collage = layui.collage;
    var pageFlip = layui.pageFlip;

    function initData() {
        var obj = {
            "currentPage": 1
        }
        pageFlip.configure(obj, collage.findCollageList, refreshTable, 'collagePageCtrl');
    }

    initData();
    var ue = UE.getEditor('content');
    function refreshTable(data, page) {
        var lines = [];
        for (var i = 0; i < data.length; i++) {
            var collageItem = data[i];
            var line = '<tr>';
            line += '<td>' + collageItem.collageId + '</td>';
            line += '<td>' + collageItem.collageName + '</td>';
            line += '<td>' + collageItem.address + '</td>';
            line += '<td>' + collageItem.content + '</td>';
            line += '<td>' + collageItem.createTime + '</td>';
            line += '<td>' + collageItem.updateTime + '</td>';
            line += '<td>' +
                '<button class="layui-btn layui-btn-normal edit" name="' + collageItem.id + '">编辑</button>' +
                // '<button class="layui-btn layui-btn-normal del" name="' + collageItem.id + '">删除</button>' +
                '<button class="layui-btn btn-disabled" name="' + collageItem.id + '">删除</button>' +
                '</td>';
            line += '</tr>';
            lines.push(line);
        }
        $('#collageTable tbody').empty().append(lines.join());
    }

    $("#addCollage ").click(function () {
        $("#addCollageForm").trigger("reset");
        layer.open({
            type: 1,
            title: "新增学院",
            area: ['750px', '800px'],
            fix: false, //不固定
            maxmin: true,
            content: $("#collageForm"),
            zIndex:1001,
            btn: ["保存", "取消"],
            yes: function () {
                var param = global.form.serializeJSON($('#addCollageForm'));
                collage.saveCollage(param, function (resp) {
                    if (resp.c == "100") {
                        layer.closeAll();
                        initData();
                    } else {
                        layer.msg(resp.m);
                    }
                })
            },
            btn2: function () {
                layer.msg("取消了");
                layer.closeAll();
            }
        });
    });
    $("#collageTable").delegate(".del", "click", function () {
        var id = $(this).attr("name");
        layer.confirm(
            "删除此学院吗？",
            ["确定", "取消"],
            function () {
                var param = {
                    "id": id,
                };
                collage.deleteCollageById(param, function (resp) {
                    if (resp.c == "100") {
                        layer.msg("删除成功");
                        layer.closeAll();
                        initData();
                    } else {
                        layer.msg(resp.m);
                    }
                })
            },
            function () {
                layer.msg("取消");
            }
        );
    });
    $("#collageTable").delegate(".edit", "click", function () {
        layer.open({
            type: 1,
            title: "编辑学院",
            area: ['750px', '800px'],
            fix: false, //不固定
            maxmin: true,
            content: $("#collageForm"),
            zIndex:1001,
            btn: ["保存", "取消"],
            yes: function () {
                var param = global.form.serializeJSON($('#addCollageForm'));
                collage.updateCollage(param, function (resp) {
                    if (resp.c == "100") {
                        layer.closeAll();
                        initData();
                    } else {
                        layer.msg(resp.m);
                    }
                })
            },
            btn2: function () {
                layer.msg("取消了");
                layer.closeAll();
            }
        });
        var id = $(this).attr("name");
        var param = {
            "id": id
        };
        collage.findCollageById(param, function (resp) {
            if (resp.c == "100") {
                var collageItem = resp.d;
                $('#addCollageForm input[name="id"]').val(collageItem.id);
                $('#addCollageForm input[name="collageId"]').val(collageItem.collageId);
                $('#addCollageForm input[name="collageName"]').val(collageItem.collageName);
                $('#addCollageForm input[name="address"]').val(collageItem.address);
                ue.setContent(collageItem.content);
            } else {
                layer.msg(resp.m);
            }
        })

    })
});